package com.apes.fn.misc.taskDownload;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.PropertiesUtils;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.Date;

/**
 * @author wuq
 * @Time 2023-6-13 10:13
 * @Description
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TaskDownload.CONST_TABLE_NAME)
public class TaskDownload extends BaseModel {
    public static final String CONST_TABLE_NAME = "task_download";

    @Id
    @Column(name = "id")
    @Comment("任务ID")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "下载任务", prefix = "TD%(yy)%(month)", padding = 9, useDateRange=true)
    private String id;

    @Column(name = "name", nullable = false, length = 120)
    @Comment("任务名称")
    private String name;

    @States(name = "任务状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "finish", name = "完成"),
            @State(id = "downloaded", name = "已下载")
    })
    @Column(name = "state", length = 8, nullable = false)
    private String state = "create";

    @Column(name = "creator_id", length = 64)
    @Comment("创建人")
    private String creator;

    @Column(name = "user_id", length = 10)
    @Comment("下载人编码")
    private Integer userId;

    @Column(name = "api", length = 64)
    @Comment("调用的接口或者查询ID")
    private String api;    // esbId 或者 queryId

    @Column(name = "ftp_Name", length = 480)
    @Comment("下载链接")
    private String ftpName;    // 下载文件名

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "params", columnDefinition = "CLOB")
    @Comment("请求参数")
    private String params;  // 请求参数或者查询条件

    @Column(name = "md5", length = 80)
    @Comment("唯一码")
    private String md5;

    @Column(name = "post_Id", length = 24)
    @Comment("岗位ID")
    private String postId;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "error", columnDefinition = "CLOB")
    @Comment("描述")
    private String error;

    @Column(name = "success", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    @Comment("是否成功")
    private boolean success = true;

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    @Transient
    private String stateName;

    public String getStateName(){
        if (StringUtils.isEmpty(this.state)) {
            return "";
        } else if ("create".equals(this.state)){
            return "执行中";
        } else {
            return "已完成";
        }
    }

    @Transient
    private String downloadUrl;

    public String getDownloadUrl(){
        if (StringUtils.isEmpty(this.ftpName)) return "";

        PropertiesUtils propertiesUtils = SpringManager.getBean(PropertiesUtils.class);
        String viewUrl = propertiesUtils.getProperty("apes.ftp.viewUrl");
        String workUrl = propertiesUtils.getProperty("apes.ftp.workDir");
        return viewUrl +workUrl + "/" + this.ftpName;
    }

}
